/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package bl;

import generating.GalaxyGenerator;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.util.ArrayList;
import java.util.LinkedList;
import resources.SpaceColors;
import resources.SpaceGeneration;

/**
 *
 * @author Alexander_Seiner
 */
public class Galaxy extends BasicGraphicElement
{
    private ArrayList<StarSystem> systems;
    private int arms = 0;
    private double size = 0.0;
    private double sizeComposer;
    private int[] armStartAngle;
    private GalaxyArm[] galaxyArms;
    private GalaxyCore galaxyCore;
    private int armRotation;
    
    public Galaxy(float posX, float posY, float width, float height, LinkedList<BasicGraphicElement> childElements) 
    {
        super(posX, posY, width, height, childElements);
        
        GalaxyGenerator galaxyGenerator = new GalaxyGenerator();
        
        arms = galaxyGenerator.getArms();
        size = galaxyGenerator.getSize();
        sizeComposer = size/this.getHeight();
        armStartAngle = galaxyGenerator.getArmStartAngles(arms);
        armRotation = galaxyGenerator.getArmRotation();
        
        galaxyCore = new GalaxyCore((float)((this.getPosX()+this.getWidth()/2) - ((size/5)/sizeComposer)/2), (float)((this.getPosY()+this.getHeight()/2) - ((size/5)/sizeComposer)/2), (float)((size/5)/sizeComposer), (float)((size/5)/sizeComposer), new LinkedList<BasicGraphicElement>(), galaxyGenerator.getCoreType());
        galaxyArms = new GalaxyArm[arms];
        for(int i = 0; i < arms; i++)
        {
            galaxyArms[i] = new GalaxyArm((((this.getPosX()+this.getWidth()/2)) + (float)(Math.cos(Math.toRadians(armStartAngle[i]))*((size/5)/sizeComposer)/2))-2, (((this.getPosY()+this.getHeight()/2)) - (float)(Math.sin(Math.toRadians(armStartAngle[i]))*((size/5)/sizeComposer)/2))-2, 1, 1, new LinkedList<BasicGraphicElement>(), armStartAngle[i], armRotation);
        }
    }
    
    @Override
    public void draw(Graphics2D g2d)
    {
        g2d.setFont(new Font("Curier New", Font.BOLD, 12));
        g2d.setColor(SpaceColors.getStarWhiteColor);
        
        g2d.drawString(size + "ly", (int)((this.getPosX()+this.getWidth()/2)-(g2d.getFontMetrics().stringWidth(size + "ly")/2)), (int)(this.getPosY() + g2d.getFont().getSize()));
        g2d.drawLine((int)(this.getPosX()+this.getWidth()/2-this.getWidth()/4), (int)(this.getPosY()+g2d.getFont().getSize()*2), (int)(this.getPosX()+this.getWidth()/2+this.getWidth()/4), (int)(this.getPosY()+g2d.getFont().getSize()*2));
        g2d.drawLine((int)(this.getPosX()+this.getWidth()/2-this.getWidth()/4), (int)(this.getPosY()+g2d.getFont().getSize()*2), (int)(this.getPosX()+this.getWidth()/2-this.getWidth()/4), (int)(this.getPosY()+g2d.getFont().getSize()*2)+15);
        g2d.drawLine((int)(this.getPosX()+this.getWidth()/2+this.getWidth()/4), (int)(this.getPosY()+g2d.getFont().getSize()*2), (int)(this.getPosX()+this.getWidth()/2+this.getWidth()/4), (int)(this.getPosY()+g2d.getFont().getSize()*2)+15);
        
        g2d.drawString("CoreType: "+ galaxyCore.getCoreType(), (int)this.getPosX(), (int)this.getPosY() + g2d.getFont().getSize());
        g2d.drawString("Amount of Arms: " + arms, (int)this.getPosX(), (int)this.getPosY() + g2d.getFont().getSize()*2);
        g2d.drawString("Size: " + size + "ly", (int)this.getPosX(), (int)this.getPosY() + g2d.getFont().getSize()*3);
        
        galaxyCore.draw(g2d);
        
        for(int i = 0; i < arms; i++)
        {
            galaxyArms[i].draw(g2d);
        }
        
        if(this.getChildElements() != null && !this.getChildElements().isEmpty())
        {
            for(BasicGraphicElement bge : this.getChildElements())
            {
                bge.draw(g2d);
            }
        }
    }

    public ArrayList<StarSystem> getSystems() {
        return systems;
    }

    public void setSystems(ArrayList<StarSystem> systems) {
        this.systems = systems;
    }
    
    public void addSystem(StarSystem system)
    {
        systems.add(system);
    }
}
